package com.example.photoview;

import javafx.scene.control.Alert;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SearchImgModel {
    private HomeController homeController=(HomeController) ControllerUtil.controllers.get(HomeController.class.getSimpleName());
    //获得找到的图片数量
    private static int foundNumbers = 0;

    //实现模糊查找并返回一个图片列表供显示，建议使用此方法
    public static ArrayList<ImageModel> fuzzySearch(String name, ArrayList<ImageModel> imageModelList) {
        //对大小写不敏感，若敏感，去除CASE_INSENSITIVE
        System.out.println("SearchImgModel -- fuzzySearch function start.");
        Pattern pattern = Pattern.compile(name, Pattern.CASE_INSENSITIVE);
        ArrayList<ImageModel> result = new ArrayList<ImageModel>();//用来存放搜索到的图片
        //System.out.println("imageModelLisy:"+imageModelList);
        if (imageModelList == null) {
            Alert alert = new Alert(Alert.AlertType.WARNING);
            alert.setTitle("提示");
            alert.setHeaderText(null);
            alert.setContentText("该目录下没有图片，搜素失败");
            alert.showAndWait();
        } else {
            for (ImageModel im : imageModelList) {// java.lang.NullPointerException
                System.out.println("56454");
                Matcher matcher = pattern.matcher(im.getImageName());
                if (matcher.find()) {//匹配imgName
                    foundNumbers++;
                    result.add(im);
                }
//            else{
//                System.out.println("SearchImgModel -- fuzzySearch -- one image is not matched");
//                System.out.println(im.getImageName());
//                System.out.println(name);
//            }
            }

        }
//未找到
        return result;

    }

    //实现精准查找并返回一个结果，需要文件的全称包括后缀
    public static ImageModel accurateSearch(String name, ArrayList<ImageModel> imageModelList) {
        //对大小写不敏感，若敏感，去除CASE_INSENSITIVE
        Pattern pattern = Pattern.compile(name, Pattern.CASE_INSENSITIVE);
        for (ImageModel im : imageModelList) {
            Matcher matcher = pattern.matcher(im.getImageName());
            if (matcher.matches()) {
                foundNumbers += 1;
                return im;
            }
        }
        //未找到
        return null;
    }

    public static void setFoundNumbers(int foundNumbers) {
        SearchImgModel.foundNumbers = foundNumbers;
    }

    public static int getFoundNumbers() {
        return foundNumbers;
    }
}
